package com.itheima79.googleplay.holder;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import com.itheima79.googleplay.R;
import com.itheima79.googleplay.bean.AppInfo;
import com.itheima79.googleplay.global.GooglePlayApplication;
import com.itheima79.googleplay.ui.anim.HeightAnim;
import com.itheima79.googleplay.ui.anim.HeightAnim.OnHeightChangeListener;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.view.ViewPropertyAnimator;

public class AppDesHolder extends BaseHolder<AppInfo> implements OnClickListener{
	private TextView tv_des,tv_author;
	private ImageView iv_des_arrow;
	
	private ScrollView scrollView;
	@Override
	public View initHolderView() {
		View view = View.inflate(GooglePlayApplication.context, R.layout.layout_detail_app_des, null);
		tv_des = (TextView) view.findViewById(R.id.tv_des);
		tv_author = (TextView) view.findViewById(R.id.tv_author);
		iv_des_arrow = (ImageView) view.findViewById(R.id.iv_des_arrow);
		
		view.setOnClickListener(this);
		return view;
	}
	
	public void setScrollView(ScrollView scrollView){
		this.scrollView = scrollView;
	}

	@Override
	public void bindData(AppInfo appInfo) {
		tv_des.setText(appInfo.getDes());
		tv_author.setText(appInfo.getAuthor());
		
		//1.让描述区域只显示5行的高度
		tv_des.setMaxLines(5);
		tv_des.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@Override
			public void onGlobalLayout() {
				tv_des.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				//获取5行的高度
				minHeight = tv_des.getHeight();
				
				//2.获取描述区域全部的高度,由于改变了tv_des的高度，会引起它重新layout
				tv_des.setMaxLines(Integer.MAX_VALUE);//让TextView显示全部内容
				tv_des.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						tv_des.getViewTreeObserver().removeGlobalOnLayoutListener(this);
						//得到全部文本的高度
						maxHeight = tv_des.getHeight();
						
						//3.让描述只显示5行的高度,
//						tv_des.setMaxLines(5);//不要使用这种方式
						LayoutParams params = tv_des.getLayoutParams();
						params.height = minHeight;
						tv_des.setLayoutParams(params);
					}
				});
			}
		});
	}
	private int minHeight;//5行文本的高度
	private int maxHeight;//全部文本的高度
	private boolean isExtend = false;//是否展开了
	private boolean isAniming = false;//是否正在执行动画
	@Override
	public void onClick(View v) {
		if(v==holderView){
			if(isAniming){
				return;
			}
			
			HeightAnim anim = null;
			if(isExtend){
				//执行收缩
				anim = new HeightAnim(tv_des, maxHeight, minHeight);
			}else {
				//执行展开动画
				anim = new HeightAnim(tv_des, minHeight, maxHeight);
			}
			anim.start(350);
			
			//设置监听器
			anim.setOnHeightChangeListener(new OnHeightChangeListener() {
				@Override
				public void onHeightChange(int animatedValue) {
					//正值其实是向上滑动,
//					scrollView.scrollBy(0,1000);
					scrollView.scrollBy(0,maxHeight-minHeight);
				}
			});
			
			//更改标记
			isExtend = !isExtend;
			
			//让箭头旋转
			ViewPropertyAnimator.animate(iv_des_arrow)
			.rotationBy(180)
			.setDuration(350)
			.setListener(new MyListener())
			.start();
		}
	}
	
	class MyListener implements AnimatorListener{
		@Override
		public void onAnimationCancel(Animator arg0) {
		}
		@Override
		public void onAnimationEnd(Animator arg0) {
			isAniming = false;
		}
		@Override
		public void onAnimationRepeat(Animator arg0) {
		}
		@Override
		public void onAnimationStart(Animator arg0) {
			isAniming = true;
		}
	}
	
	
	
}
